#include "sessionfriendarea.h"

SessionFriendArea::SessionFriendArea()
{
    this->setStyleSheet("QWidget {background-color:rgb(230,230,230);}");
    //开启滚动条效果
    this->setWidgetResizable(true);

    //设置滚动条样式
    //禁用水平滚动条
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");

    //设置垂直滚动条样式,
    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:2px; background-color:rgb(191,189,187);}");

    //核心widget实例化
    container = new QWidget(this);
    container->setFixedWidth(200); //设置定宽
    this->setWidget(container);    //生效container

    //创建垂直布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignTop);
    container->setLayout(layout);

#ifdef test
    //随便测试一下
    for(int i = 0; i < 50;i++)
    {
        addSessionFriendItem(SessionFriendItem::ItemType::SessionItemType,QString("1000") + QString::number(i),QIcon(":/image/logo.png"),"testname" + QString::number(i),"textmessage"+ QString::number(i));
    }
    addSessionFriendItem(SessionFriendItem::ItemType::ApplyItemType,QString("1000") + QString::number(9999),QIcon(":/image/logo.png"),"testname" + QString::number(9999),"textmessage"+ QString::number(9999));

#endif
}
////////////////////////////////////////////////////
/// SessionFriendItem 操作函数
////////////////////////////////////////////////////
void SessionFriendArea::addSessionFriendItem(SessionFriendItem::ItemType itemType, const QString &id, const QIcon& avatar, const QString &name, const QString &text)
{
    //生成一个空的item,以备使用
    SessionFriendItem* newitem = nullptr;

    switch(itemType)
    {
    case SessionFriendItem::ItemType::SessionItemType :
        newitem = new SessionItem(this, id, avatar, name, text);
        break;
    case SessionFriendItem::ItemType::FriendItemType :
        newitem = new FriendItem(this, id, avatar, name, text);
        break;
    case SessionFriendItem::ItemType::ApplyItemType :
        newitem = new ApplyItem(this, id, avatar, name);
        break;
    }
    container->layout()->addWidget(newitem);
}

void SessionFriendArea::deleteSessionFriendItem()
{
    LOG()<<"void SessionFriendArea::deleteSessionFriendItem() 尚未实现";
}

void SessionFriendArea::clear()
{
    //获取layout
    QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(container->layout());
    for(int i = layout->count()- 1; i>=0 ; i--)
    {
        //将元素从布局管理器中移除
        QLayoutItem* item = layout->takeAt(i);

        //释放item中QWidget的内存占用
        if(item->widget()) delete item->widget(); //释放SessionFriendItem

        //释放item自身内存占用
        delete item;
    }
}

void SessionFriendArea::clickItem(int index)
{
    if (index < 0 || index >= container->layout()->count()) {
        LOG() << "点击元素的下标超出范围! index=" << index;
        return;
    }
    QLayoutItem* layoutItem = container->layout()->itemAt(index);
    if (layoutItem == nullptr || layoutItem->widget() == nullptr) {
        LOG() << "指定的元素不存在! index=" << index;
        return;
    }
    SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(layoutItem->widget());
    item->select();
}

































